package com.ironman.imes.device.api.model;

import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/demo")
public class DemoController {

    @Resource
    RedissonClient redissonClient;


    @GetMapping
    public void test() {
        RLock lock = redissonClient.getLock("name");
        try {
            if (lock.tryLock()) {
                log.info("获取锁成功");
            }
        } finally {
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
    }
}
